/*
 * FriendsServer.h
 *
 *  Created on: 2013. 8. 13.
 *      Author: the2
 */

#ifndef FRIENDSSERVER_H_
#define FRIENDSSERVER_H_

#include <Dispatcher/Session.h>
#include <Dispatcher/Timer.h>
#include <Dispatcher/NSession/State.h>
#include <Network/Static.h>
#include "../../../protocol/iFriends.h"
#include "../macro.h"

#include "Session.h"

namespace ProjectZ { namespace NProxy {

class FriendsServer : public Session::IOwner, public::Dispatcher::Timer::IObserver
{
public:
	class Msg
	{
	public:
		static void OnOpenAck(Dispatcher::Session* pSession, ::Network::Packet * pPacket );
		static void OnCloseAck(Dispatcher::Session* pSession, ::Network::Packet * pPacket );

		static void OnSocialMyInfoAck(Dispatcher::Session* pSession, ::Network::Packet * pPacket );
		static void OnSocialUserSeqAck(Dispatcher::Session* pSession, ::Network::Packet * pPacket );
		static void OnGameUserSeqAck(Dispatcher::Session* pSession, ::Network::Packet * pPacket );
		static void OnSocialFriendsAck(Dispatcher::Session* pSession, ::Network::Packet * pPacket );
		static void OnGameFriendsAck(Dispatcher::Session* pSession, ::Network::Packet * pPacket );
		static void OnInviteNoAppAck(Dispatcher::Session* pSession, ::Network::Packet * pPacket );
		static void OnNewsCountAck(Dispatcher::Session* pSession, ::Network::Packet * pPacket);
		static void OnNewsAck(Dispatcher::Session* pSession, ::Network::Packet * pPacket );
		static void OnSimpleFriendsAck(Dispatcher::Session* pSession, ::Network::Packet * pPacket );
		static void OnMainListAck(Dispatcher::Session* pSession, ::Network::Packet * pPacket );
		static void OnUpdateProfileAck(Dispatcher::Session* pSession, ::Network::Packet * pPacket );
		static void OnUpdateStatusAck(Dispatcher::Session* pSession, ::Network::Packet * pPacket );
		static void OnInviteBlockAck(Dispatcher::Session* pSession, ::Network::Packet * pPacket );
		static void OnFriendShipBlockAck(Dispatcher::Session* pSession, ::Network::Packet * pPacket );
		static void OnSendHeartAck(Dispatcher::Session* pSession, ::Network::Packet * pPacket );
		static void OnRecommendAck(Dispatcher::Session* pSession, ::Network::Packet * pPacket );
		static void OnBoastAck(Dispatcher::Session* pSession, ::Network::Packet * pPacket );
		static void OnUseBuffAck(Dispatcher::Session* pSession, ::Network::Packet * pPacket );
		static void OnGameFriendAck(Dispatcher::Session* pSession, ::Network::Packet * pPacket );
		static void OnConfirmFriendAck(Dispatcher::Session* pSession, ::Network::Packet * pPacket );
		static void OnRequestConnectAck(Dispatcher::Session* pSession, ::Network::Packet * pPacket );
		static void OnConfirmNewsAck(Dispatcher::Session* pSession, ::Network::Packet * pPacket );
		static void OnDeleteFriendAck(Dispatcher::Session* pSession, ::Network::Packet * pPacket );
		static void OnAllowFriendAck(Dispatcher::Session* pSession, ::Network::Packet * pPacket );
		static void OnInviteCountAck(Dispatcher::Session* pSession, ::Network::Packet * pPacket );
		static void OnFlushFRUserInfoAck(Dispatcher::Session* pSession, ::Network::Packet * pPacket );
		static void OnGachyaBoxAck(Dispatcher::Session* pSession, ::Network::Packet * pPacket );
		static void OnLastWeekRankInfoAck(Dispatcher::Session* pSession, ::Network::Packet * pPacket );
		static void OnGetHeartCountAck(Dispatcher::Session* pSession, ::Network::Packet * pPacket );
		static void OnAddHeartCountAck(Dispatcher::Session* pSession, ::Network::Packet * pPacket );
		static void OnSendStaminaAck(Dispatcher::Session* pSession, ::Network::Packet * pPacket );
		static void OnConfirmNewsV2Ack(Dispatcher::Session* pSession, ::Network::Packet * pPacket);
	};

public:
	class State : public ::Dispatcher::NSession::State
	{
	public:
		State()
		{
			AddCommand( FriendsOpenAck::MSG_ID, Msg::OnOpenAck);
			AddCommand( FriendsCloseAck::MSG_ID, Msg::OnCloseAck);

			AddCommand( SocialMyInfoAck::MSG_ID, Msg::OnSocialMyInfoAck);
			AddCommand( SocialUserSeqAck::MSG_ID, Msg::OnSocialUserSeqAck);
			AddCommand( GameUserSeqAck::MSG_ID, Msg::OnGameUserSeqAck);
			AddCommand( SocialFriendsAck::MSG_ID, Msg::OnSocialFriendsAck);
			AddCommand( GameFriendsAck::MSG_ID, Msg::OnGameFriendsAck);
			AddCommand( InviteNoAppAck::MSG_ID, Msg::OnInviteNoAppAck);
			AddCommand( NewsCountAck::MSG_ID, Msg::OnNewsCountAck);
			AddCommand( NewsInfoAck::MSG_ID, Msg::OnNewsAck);
			AddCommand( SimpleFriendsAck::MSG_ID, Msg::OnSimpleFriendsAck);
			AddCommand( MainListAck::MSG_ID, Msg::OnMainListAck);
			AddCommand( UpdateProfileAck::MSG_ID, Msg::OnUpdateProfileAck);
			AddCommand( UpdateStatusAck::MSG_ID, Msg::OnUpdateStatusAck);
			AddCommand( InviteBlockAck::MSG_ID, Msg::OnInviteBlockAck);
			AddCommand( FriendShipBlockAck::MSG_ID, Msg::OnFriendShipBlockAck);
			AddCommand( SendHeartAck::MSG_ID, Msg::OnSendHeartAck);
			AddCommand( RecommendAck::MSG_ID, Msg::OnRecommendAck);
			AddCommand( BoastAck::MSG_ID, Msg::OnBoastAck);
			AddCommand( UseBuffAck::MSG_ID, Msg::OnUseBuffAck);
			AddCommand( GameFriendAck::MSG_ID, Msg::OnGameFriendAck);
			AddCommand( ConfirmFriendAck::MSG_ID, Msg::OnConfirmFriendAck);
			AddCommand( RequestConnectAck::MSG_ID, Msg::OnRequestConnectAck);
			AddCommand( ConfirmNewsAck::MSG_ID, Msg::OnConfirmNewsAck);
			AddCommand( DeleteFriendAck::MSG_ID, Msg::OnDeleteFriendAck);
			AddCommand( AllowFriendAck::MSG_ID, Msg::OnAllowFriendAck);
			AddCommand( InviteCountAck::MSG_ID, Msg::OnInviteCountAck);
			AddCommand( FlushFRUserInfoAck::MSG_ID, Msg::OnFlushFRUserInfoAck);
			AddCommand( GachyaBoxAck::MSG_ID, Msg::OnGachyaBoxAck);
			AddCommand( LastWeekRankInfoAck::MSG_ID, Msg::OnLastWeekRankInfoAck);
			AddCommand( GetHeartCountAck::MSG_ID, Msg::OnGetHeartCountAck);
			AddCommand( AddHeartCountAck::MSG_ID, Msg::OnAddHeartCountAck);
			AddCommand( SendStaminaAck::MSG_ID, Msg::OnSendStaminaAck);
			AddCommand( ConfirmNewsV2Ack::MSG_ID, Msg::OnConfirmNewsV2Ack);
		}

		~State() {}
	};

public:
	FriendsServer();
	virtual ~FriendsServer();

	bool Connect(int idx, std::string strIP, int port);
	void	OnDisconnect();
	virtual bool OnTime(int id, void* param);

	template<class T> void SendMessage(const T& msg)
	{
		::Network::Packet* pPacket = ::Network::GETPACKETPOOL()->SEND_NEW();
		char* pBuf = pPacket->GetOffsetPtr();
		if(false == msg.Store(&pBuf))
		{
			::Network::GETPACKETPOOL()->SEND_DEL(pPacket);
			return;
		}
		pPacket->SetLen(msg.Size());
		pPacket->MakePacketNoTail(msg.MSG_ID);

		DEBUGLOG("$$ %s MSG_ID=%d SEQ=%d",
				__FUNCTION__, msg.MSG_ID, msg.seq);
		_pSession->SendPacket(pPacket, true);
	}

	int 		GetIdx() { return _idx; }

private:
	void	setTime();

private:
	//::Dispatcher::Session * _pSession;
	Session * _pSession;
	bool		_bConnect;
	int 		_idx;
	::Network::Session::Info _clsInfo;
};

} /*NProxy*/ } /*ProjectZ*/


#endif /* FRIENDSSERVER_H_ */
